<template>
  <div class="app-container" style="display:flex;">
    <question-tree style="margin-left:0px;" @changeTree="changeTree" />
    <div style="flex:1;margin-left: 10px;">
      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
        <el-form-item label="方案名称" prop="schemeName">
          <el-input
            v-model="queryParams.schemeName"
            placeholder="请输入方案名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="方案状态">
          <el-select v-model="queryParams.state" placeholder="请选择">
            <el-option
              label="所有"
              value="0"
            />
            <el-option
              label="进行中"
              value="1"
            />
            <el-option
              label="已结束"
              value="2"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="方案描述" prop="description">
          <el-input
            v-model="queryParams.description"
            placeholder="请输入方案描述"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="生成日期" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生成日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['decision:scheme:examine']"
            type="primary"
            plain
            icon="el-icon-check"
            size="mini"
            :disabled="single"
            @click="handleExamine"
          >审核</el-button>
          <el-button
            v-hasPermi="['decision:scheme:report']"
            type="primary"
            plain
            icon="el-icon-upload2"
            size="mini"
            :disabled="single"
            @click="handleReport"
          >方案上报</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['decision:scheme:edit']"
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['decision:scheme:remove']"
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['decision:scheme:export']"
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
      </el-row>

      <el-table v-loading="loading" :data="schemeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="方案ID" align="center" prop="schemeId" />
        <el-table-column label="方案名称" align="center" prop="schemeName" />
        <el-table-column label="所属预案ID" align="center" prop="taskId" />
        <el-table-column label="方案状态" align="center" prop="version" />
        <el-table-column label="方案描述" align="center" prop="description" />
        <el-table-column label="计划数据接口地址" align="center" prop="planApi" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['decision:scheme:edit']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              v-hasPermi="['decision:scheme:remove']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改决策方案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="方案名称" prop="schemeName">
          <el-input v-model="form.schemeName" placeholder="请输入方案名称" />
        </el-form-item>
        <el-form-item label="所属预案ID" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入所属预案ID" />
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="方案描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入方案描述" />
        </el-form-item>
        <el-form-item label="计划数据接口地址" prop="planApi">
          <el-input v-model="form.planApi" placeholder="请输入计划数据接口地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScheme, getScheme, delScheme, addScheme, updateScheme,report,schemeId } from '@/api/decision/scheme'
import questionTree from '@/components/createPlan/questionTree'
export default {
  name: 'Scheme',
  components: { questionTree: questionTree },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 决策方案表格数据
      schemeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schemeName: null,
        state: '0',
        description: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },

  created() {
    this.getList()
  },
  methods: {
    /** 查询决策方案列表 */
    getList() {
      this.loading = true
      listScheme(this.queryParams).then(response => {
        this.schemeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    //审核
    handleExamine(){
      const schemeId = this.ids
      schemeId(schemeId).then((response)=>{
        this.$modal.msgSuccess('审核成功')
      })
    },
    //方案上报
    handleReport(){
      const schemeId = this.ids
      report(schemeId).then((response)=>{
        this.$modal.msgSuccess('上报成功')
      })
    },
    changeTree(val) {
      this.queryParams.schemeName = val.name
      this.getList()
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        schemeId: null,
        schemeName: null,
        taskId: null,
        version: null,
        description: null,
        createTime: null,
        planApi: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.schemeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加决策方案'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      debugger
      this.reset()
      const schemeId = row.schemeId || this.ids
      getScheme(schemeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改决策方案'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.schemeId != null) {
            updateScheme(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addScheme(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const schemeIds = row.schemeId || this.ids
      this.$modal.confirm('是否确认删除决策方案编号为"' + schemeIds + '"的数据项？').then(function() {
        return delScheme(schemeIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('decision/scheme/export', {
        ...this.queryParams
      }, `scheme_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
